var fireproxy = function () {
	var pref = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);
	var log = Components.classes['@mozilla.org/consoleservice;1'].getService(Components.interfaces.nsIConsoleService);
	var proxy_struct = {
		type : {
			type : 'int',
			key : 'network.proxy.type'
		},
		share : {
			type: 'bool',
			key: 'network.proxy.share_proxy_settings'
		},
		host : {
			type : 'str',
			key : 'network.proxy.http'
		},
		port : {
			type : 'int',
			key : 'network.proxy.http_port'
		},
		shost : {
			type : 'str',
			key : 'network.proxy.ssl'
		},
		sport : {
			type : 'int',
			key : 'network.proxy.ssl_port'
		},
		excl : {
			type : 'str',
			key : 'network.proxy.no_proxies_on'
		},
		ftp : {
			type : 'str',
			key : 'network.proxy.ftp'
		},
		ftp_port : {
			type : 'int',
			key : 'network.proxy.ftp_port'
		},
		socks : {
			type : 'str',
			key : 'network.proxy.socks'
		},
		socks_port : {
			type : 'int',
			key : 'network.proxy.socks_port'
		},
		gopher : {
			type : 'str',
			key : 'network.proxy.gopher'
		},
		gopher_port : {
			type : 'int',
			key : 'network.proxy.gopher_port'
		}
	};
	
	var getPrefValue = function(id) {
		switch(id.type){
			case 'int':
				return pref.getIntPref(id.key)
			case 'str':
				return pref.getCharPref(id.key);
			default:
				return undefined;
		}
	};
	
	var getElement = function(id) {
		return document.getElementById(id);
	};

	var debug = function (msg) {
		log.logStringMessage(msg);
	};
	
	var debugMembers = function(obj) {
		for(k in obj) {
			debug(k + "::" + obj[k]);
		}
	};
	
	var todo = function() {
		debug('Not Implemented');
	};

	var guiVisit = function(visitor) {
		for(k in proxy_struct) {
			var guiElement = getElement("gui." + k);
			if(guiElement) {
				visitor(guiElement);
			}
		}
	};

	var setVisible = function(id, state) {
			var item = getElement(id);
			// debug(id + "::" + item.getAttribute('collapsed'));
			if(state) {
				item.removeAttribute('collapsed');
			} else {
				item.setAttribute('collapsed', 'true');
			}
	};
	
	var loadProxies = function() {
			var lst = getElement("proxyList")
			lst.removeAllItems();
			for(k in vproxy) {
				lst.appendItem(k, k);
			}
			// debug('selectedIndex=' + lst.selectedIndex);
			if(lst.itemCount > 0) {
				lst.selectedIndex = 0;
			}
	};

	var setBrowserProxy = function(proxy) {
		for(k in proxy_struct) {
			var id = proxy_struct[k];
			debug(k + "::" + id.key + "::" + proxy[k]);
			switch(id.type){
				case 'int':
					pref.setIntPref(id.key, proxy[k]);
				break;
				case 'str':
					pref.setCharPref(id.key, proxy[k]);
				break;
				case 'bool':
					pref.setBoolPref(id.key, proxy[k]);
				break;
			}
		}
	};

	var setProxyEdit = function(state) {
		guiVisit(
			function(guiElement) {
				if( state ) {
					guiElement.removeAttribute('disabled');
				}
				else {
					guiElement.setAttribute('disabled', 'true');
				}
			}
		);
	};
	
	return {
		run : function () {
            //  debug("this blah!!!");
			str = "";
			for(i in pref){
				str += i + "\n";
			}
			//  debug(str);
			for(k in proxy_struct){
				debug(k + "::" + proxy_struct[k].type + "::" + proxy_struct[k].key + "::" + getPrefValue(proxy_struct[k]));
			}
		},
		openSetupWindow : function() {
			var dlg = window.openDialog("chrome://fireproxy/content/setup.xul","fireproxySetup","chrome, centerscreen, modal");
			dlg.focus();
		},
		onLoad : function() {
			alert('plE=' + getElement('proxyList').editable);
			loadProxies();
			setProxyEdit(false);
		},
		onUnload : function() {
			todo();
		},
		onNewProxy : function() {
			//todo();
			setVisible('proxy.box.edit', false);
			setVisible('proxy.box.save', true);
		},
		onEditProxy : function() {
			//todo();
			setVisible('proxy.box.edit', false);
			setVisible('proxy.box.save', true);
			setProxyEdit(true);
		},
		onSaveProxy : function() { // save the currently edited
			//todo();
			setVisible('proxy.box.edit', true);
			setVisible('proxy.box.save', false);
		},
		onCancelProxy : function() { // cancel editing ...
			//todo();
			setVisible('proxy.box.edit', true);
			setVisible('proxy.box.save', false);
			loadProxies();
			setProxyEdit(false);
		},
		onDeleteProxy : function() { // delete the selected proxy
			todo();
		},
		onSetProxyToBrowser : function() {
			var selectedProxy = getElement("proxyList").selectedItem;
			if(selectedProxy != null && selectedProxy.value) {
				var proxy = vproxy[selectedProxy.value];
				// debugMembers(proxy);
				setBrowserProxy(proxy); // set the proxy to the browser
			}
			// todo();
		},
		onSetNoProxy : function() {
			setBrowserProxy(NO_PROXY);
			// todo();
		},
		onSelectProxy : function(selectedProxy) {
			if(selectedProxy != null && selectedProxy.value) {
				var lst = getElement("proxyList")
				// debug('selectedIndex=' + lst.selectedIndex);
				var proxy = vproxy[selectedProxy.value];
				// debugMembers(proxy);
				for(k in proxy_struct) {
					if(getElement("gui." + k)) {
						getElement("gui." + k).value = proxy[k];
					}
				}
				//debugMembers(getElement("gui.host"));
			}
		}
	};
}();

var NO_PROXY = {
	type : 0,
	share : false,
	host : '',
	port : 0,
	shost: '',
	sport: 0,
	excl : '',
	ftp : '',
	ftp_port : 0,
	socks : '',
	socks_port : 0,
	gopher : '',
	gopher_port : 0
};

var vproxy = {
	proxy1 : {
		type : 1,
		share : false,
		host : '127.0.0.1',
		port : 8080,
		shost: '127.0.0.1',
		sport: 8080,
		excl : '127.0.0.1,localhost',
		ftp : '',
		ftp_port : 0,
		socks : '',
		socks_port : 0,
		gopher : '',
		gopher_port : 0
	},
	proxy2 : {
		type : 1,
		share : false,
		host : '127.0.0.1',
		port : 9999,
		shost: '127.0.0.112',
		sport: 9999,
		excl : '127.0.0.1,localhost',
		ftp : '',
		ftp_port : 0,
		socks : '',
		socks_port : 0,
		gopher : '',
		gopher_port : 0
	},
	proxy3 : {
		type : 1,
		share : false,
		host : '172.16.0.1',
		port : 8080,
		shost: '172.16.1.1',
		sport: 1080,
		excl : '127.0.0.1,localhost,172.16.0.0/16',
		ftp : '',
		ftp_port : 0,
		socks : '',
		socks_port : 0,
		gopher : '',
		gopher_port : 0
	}
};
